//
//  ZYCTeacherLeaveDatePickerView.m
//  ZDSC
//
//  Created by 张威 on 2021/10/22.
//

#import "ZYCTeacherLeaveDatePickerView.h"
#import "UniteModel.h"
#import "UIViewExt.h"

#define kDateViewH 258
#define kPickViewH 218

@interface ZYCTeacherLeaveDatePickerView ()

@property (nonatomic,strong) UILabel *tipLB;

@end
@implementation ZYCTeacherLeaveDatePickerView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.hidden = YES;
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBlankAction:)]];
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.2];
        _dates = [UniteModel getRentDates];
        _hours = [UniteModel getTodayRentHours];
        _minutes = [UniteModel getNextHoursMinures];
        
        _picker = [[UIPickerView alloc]init];
        _picker.backgroundColor = [UIColor whiteColor];
        _picker.delegate = self;
        _picker.dataSource = self;
//        _picker.frame = CGRectMake(0, 0, self.bounds.size.width, kPickViewH);
        _picker.frame = CGRectMake(0, self.bounds.size.height, self.bounds.size.width, kPickViewH);
        [self addSubview:_picker];
        
        UIView *containView = [UIView new];
        containView.backgroundColor = [UIColor whiteColor];
        [self addSubview:containView];
        
        UILabel *tipLB = [UILabel new];
        tipLB.backgroundColor = [UIColor whiteColor];
        tipLB.font = [UIFont systemFontOfSize:22];
        tipLB.textAlignment = NSTextAlignmentCenter;
        tipLB.text = @"请选择时间";
        tipLB.textColor = HEX_RGB(@"#333333");
        [containView addSubview:tipLB];
        _tipLB = tipLB;
        
        UIButton *cancelBtn = [UIButton new];
        [cancelBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        cancelBtn.titleLabel.font = [UIFont systemFontOfSize:20];
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [cancelBtn setBackgroundColor:[UIColor whiteColor]];
        cancelBtn.tintColor = [UIColor blackColor];
        [cancelBtn addTarget:self action:@selector(cancelBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [containView addSubview:cancelBtn];
        
        
        UIButton *confirmBtn = [UIButton new];
        [confirmBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        confirmBtn.titleLabel.font = [UIFont systemFontOfSize:20];
        [confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
        [confirmBtn setBackgroundColor:[UIColor whiteColor]];
        confirmBtn.tintColor = [UIColor blackColor];
        [confirmBtn addTarget:self action:@selector(confirmBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [containView addSubview:confirmBtn];
        
        UIView *lineView = [UIView new];
        lineView.backgroundColor = HEX_RGB(@"eeeeee");
        [containView addSubview:lineView];
        
        [containView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_picker.mas_top).offset(-35);
            make.left.equalTo(_picker);
            make.right.equalTo(_picker);
            make.height.equalTo(@50);
        }];
        
        [tipLB mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(containView);
            make.top.equalTo(containView);
            make.bottom.equalTo(containView);
            make.width.equalTo(@200);
        }];
        
        [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(containView).offset(16);
            make.top.equalTo(containView);
            make.bottom.equalTo(containView);
        }];
        
        [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(containView).offset(-16);
            make.top.equalTo(containView);
            make.bottom.equalTo(containView);
        }];
        
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.mas_equalTo(containView);
            make.height.mas_equalTo(1);
        }];
        
    }
    return self;
}

- (void)tapBlankAction:(UITapGestureRecognizer *)tap{
    CGPoint tapP = [tap locationInView:tap.view];
    
    if (!CGRectContainsPoint(_picker.frame, tapP)) {
        [self hide];
    }
}

- (void)hide
{
    [UIView animateWithDuration:.2 animations:^{
        
        _picker.top += kPickViewH;
        
        _tipLB.top = _picker.top-25;
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        self.hidden = YES;
    }];
}

- (void)show
{
    [self layoutIfNeeded];
    self.hidden = NO;
    
    [UIView animateWithDuration:.2 animations:^{
        _picker.top -= kPickViewH;
        _tipLB.top = _picker.top-25;
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
    }];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _tipLB.text = _tipString;
}

#pragma mark - UIPickerViewDataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    switch (component) {
        case 0:
            return self.dates.count;
            break;
        case 1:
            return self.hours.count;
            break;
        case 2:
            return self.minutes.count;
            break;
        default:
            return 0;
            break;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    switch (component) {
        case 0:
        {
            return self.dates[row];
        }
            
        case 1:
        {
            return self.hours[row];
        }
        case 2:
        {
            return self.minutes[row];
        }
        default: return @"";
    }
}


#pragma mark - UIPickerViewDelegate

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.minimumScaleFactor = 18.0;
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont systemFontOfSize:24]];
        [pickerLabel setTextColor:HEX_RGB(@"333333")];
    }
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    switch (component) {
        case 0:{
            if(row == 0){
                _hours = [UniteModel getTodayRentHours];
                _minutes = [UniteModel getNextHoursMinures];
                [_picker reloadComponent:1];
                [_picker reloadComponent:2];
                [_picker selectRow:0 inComponent:1 animated:YES];
                [_picker selectRow:0 inComponent:2 animated:YES];
            }else if(row == self.dates.count-1){
                NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
                [formatter setDateFormat:@"HH"];
                NSDate *date = [NSDate date];
                NSString *curDate = [NSString stringWithFormat:@"%@",[formatter stringFromDate:date]];
                NSUInteger nowHours = [curDate integerValue];
                _hours = [UniteModel getLastDayRentHours];
                [_picker reloadComponent:1];
                [_picker selectRow:nowHours inComponent:1 animated:YES];

                if ([_picker selectedRowInComponent:1] == self.hours.count -1) {
                    _minutes = [UniteModel getLastHoursMinures];
                }else{
                    _minutes = [UniteModel getNormalHoursMinures];
                }
                [_picker reloadComponent:2];
                
//                NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
//                [formatter setDateFormat:@"HH"];
//                NSDate *date = [NSDate date];
//                NSString *curDate = [NSString stringWithFormat:@"%@",[formatter stringFromDate:date]];
//                NSUInteger nowHours = [curDate integerValue];
                
                [_picker selectRow:nowHours inComponent:1 animated:YES];
                
            }else{
                _hours = [UniteModel getRentHours];
                _minutes = [UniteModel getRentMinutes];
                [_picker reloadComponent:1];
                [_picker reloadComponent:2];
                
                NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
                [formatter setDateFormat:@"HH"];
                NSDate *date = [NSDate date];
                NSString *curDate = [NSString stringWithFormat:@"%@",[formatter stringFromDate:date]];
                NSUInteger nowHours = [curDate integerValue];
                
                [_picker selectRow:nowHours inComponent:1 animated:YES];
                
            }
            
            NSString *date = _dates[row];
            if([date isEqualToString:@"今天"]){
                date = [UniteModel  getLaterDateWithDays:0];
            }else if ([date isEqualToString:@"明天"]){
                date = [UniteModel  getLaterDateWithDays:1];
            }
            
            NSString *hour = _hours[[_picker selectedRowInComponent:1]];
            NSString *minu = _minutes[[_picker selectedRowInComponent:2]];
            
            NSString *time = [NSString stringWithFormat:@"%@:%@",hour,minu];
            _currentDate = date;
            _currentTime = time;
            
            break;
        }
        case 1:
        {
            if(([_picker selectedRowInComponent:0] == 0) && row == 0){
                _minutes = [UniteModel getNextHoursMinures];
                [_picker reloadComponent:2];
                [_picker selectRow:0 inComponent:2 animated:YES];
            }else if(row == self.hours.count-1&&([_picker selectedRowInComponent:0] == self.dates.count - 1)){
                _minutes = [UniteModel getLastHoursMinures];
                [_picker reloadComponent:2];
            }
            else{
                _minutes = [UniteModel getNormalHoursMinures];
                [_picker reloadComponent:2];
            }
            
            NSString *date = _dates[[_picker selectedRowInComponent:0]];
            if([date isEqualToString:@"今天"]){
                date = [UniteModel  getLaterDateWithDays:0];
            }else if ([date isEqualToString:@"明天"]){
                date = [UniteModel  getLaterDateWithDays:1];
            }
            
            NSString *hour = _hours[row];
            NSString *minu = _minutes[[_picker selectedRowInComponent:2]];
            
            NSString *time = [NSString stringWithFormat:@"%@:%@",hour,minu];
            _currentDate = date;
            _currentTime = time;

            break;
        }
        case 2:{
            NSString *date = _dates[[_picker selectedRowInComponent:0]];
            if([date isEqualToString:@"今天"]){
                date = [UniteModel  getLaterDateWithDays:0];
            }else if ([date isEqualToString:@"明天"]){
                date = [UniteModel  getLaterDateWithDays:1];
            }else{
            
            
            }
            
            NSString *hour = _hours[[_picker selectedRowInComponent:1]];
            NSString *minu = _minutes[row];
            
            NSString *time = [NSString stringWithFormat:@"%@:%@",hour,minu];
            _currentDate = date;
            _currentTime = time;
            break;
        }
        default:
            break;
    }
    
}

- (void)cancelBtnAction
{
    [self hide];
    if (_cancelAction) {
        _cancelAction();
    }
    
    
}
- (void)confirmBtnAction
{
    if(_SelectBlock){
        if (!_currentDate) {
            [self pickerView:self.picker didSelectRow:0 inComponent:0];
        }
        _SelectBlock(_currentDate,_currentTime);
    }
    [self hide];
}
@end
